print("hotfix begin")

local function get_upvalues(f)
	local u = {}
	if not f then
		return u
	end
	local i = 1
	while true do
		local name, value = debug.getupvalue(f, i)
		if name == nil then
			return u
		end
		u[name] = value
		i = i + 1
	end
end

local skynet = require 'skynet'
local proto = get_upvalues(skynet.register_protocol).proto
local log = require 'logger'
local dump = require 'util'.dump
local function replace_upvalue(func, uvname, value)
	local i = 1
	while true do
		local name, uv = debug.getupvalue(func, i)
		if name == nil then
			break
		end
		if name == uvname then
			if value then
				debug.setupvalue(func, i, value)
			end
			return uv
		end
		i = i + 1
	end
end

package.loaded['protoloader'] = nil
local pb = require 'protoloader'
log.info("proto[skynet.PTYPE_CLIENT]", dump(proto[skynet.PTYPE_CLIENT]))

proto[skynet.PTYPE_CLIENT].unpack = function(bytes, sz)
	local str = skynet.tostring(bytes, sz)
	local msgname, msg = pb.decode(str, sz)
	return msgname, msg
end

log.info("proto[skynet.PTYPE_CLIENT]", dump(proto[skynet.PTYPE_CLIENT]))
-- nc 127.0.0.1 8000
-- help
-- list
-- call addr 'cmd'  : cmd 需要 '' 或 ""
-- inject addr file : 文件使用全路径

-- call 0000003c 'test'
-- inject 0000003c /srv/samba/Pool/server2/game/hotfix_examples/inject_checklogin.lua
-- inject 0000001d examples/inject_bigmap.lua
print("hotfix end")